package com.example.administrator.sixangleview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.sixangleview.UtilTools;

import java.util.HashMap;
import java.util.Map;

public class SixangleViewGroup extends ViewGroup {
    public static int LENGTH;
    private static final int SPACE = 30; // 六边形与y边框的距离
    private int centerX;
    private int centerY;
    private int mLength;
    private Map<Integer, Double> radianMap;
    private int half;
    private double RADIAN;
    private Context mContext;

    public SixangleViewGroup(Context context) {
        super(context);
        mContext = context;
        radianMap = new HashMap<>();
        init();
        half = LENGTH / 2;
        RADIAN = Math.PI / 180;
    }

    public SixangleViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        radianMap = new HashMap<>();
        init();
        half = LENGTH / 2;
        RADIAN = Math.PI / 180;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getWidth();
        int height = getHeight();

        centerX = width / 2;
        centerY = height / 2;
        mLength = (width - SPACE * 2 - LENGTH) / 2;

        double radian;
        int x, y;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (i == 0) {
                child.layout(centerX - half, centerY - half, centerX + half, centerY + half);
            } else {
                radian = (i - 1) * 60d * RADIAN;
                radianMap.put(i, radian);
                x = centerX + (int) (mLength * Math.cos(radian));
                y = centerY + (int) (mLength * Math.sin(radian));
                child.layout(x - half, y - half, x + half, y + half);
            }
        }
    }

    public void changeChildLayout(double alpha) {
        synchronized (this) {
            SixangleImageViews child;
            double preRadian;
            double radian;
            int x, y;
            for (int i = 0; i < getChildCount(); i++) {
                child = (SixangleImageViews) getChildAt(i);
                if (i == 0) {
                    child.addDegrees(alpha);
                    child.setRotate(true);
                    child.postInvalidate();
                } else {
                    preRadian = radianMap.get(i);
                    radian = preRadian + alpha * RADIAN;
                    if (radian >= 360) radian = 0 * RADIAN;
                    radianMap.put(i, radian);
                    x = centerX + (int) (mLength * Math.cos(radian));
                    y = centerY + (int) (mLength * Math.sin(radian));
                    child.setRotate(false);
                    child.layout(x - half, y - half, x + half, y + half);
                }
            }
        }
    }

    private void init() {
        if(mContext == null)
            mContext = getContext();
        int screenWidth = UtilTools.getScreenWidth(mContext);
        float length =  (screenWidth - 2 * SPACE) / 4.2f;
        LENGTH = (int) length;
    }

}